package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.entity.Audit;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Slf4j
@Service
@Transactional
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public void sava(ApplicationSaveParam param) {
        Application application = new Application();
        BeanUtils.copyProperties(param,application);
        application.setStatus(ApplicationStatusEnum.PENGDING.getCode());
        application.setCreateTime(new Date());
        applicationMapper.insert(application);
        auditService.insert(application);
    }

    //查询
    @Override
    public List<ApplicationVO> select(ApplicationQuery query) {
        log.debug("{}",query);
        List<ApplicationVO> applicationVO=  applicationMapper.select(query);
        //根据申请单id查询集合
        for (ApplicationVO vo:applicationVO){
         List<Audit> list = auditMapper.selectByApplicationId(vo.getId());
            List<Long> auditUserIdList =new ArrayList<>();
            List<String> auditUsernameList =new ArrayList<>();
            for (Audit audit:list){
                //获取审批人id
                auditUserIdList.add(audit.getAuditUserId());
                //获取审批人名字
                UserVO userVO = userMapper.selectById(audit.getAuditUserId());
                auditUsernameList.add(userVO.getUsername());
            }
            StringJoiner stringJoiner = new StringJoiner(",");
            for (String username:auditUsernameList){
                stringJoiner.add(username);
            }
            vo.setAuditUsernameList(stringJoiner.toString());
            vo.setAuditUserIdList(auditUserIdList);
        }
        return applicationVO;
    }

    /**
     * 修改申请表状态并且删除审批表
     * @param id
     */
    @Override
    public void update(Long id) {
        //修改申请表
        Application app = new Application();
        app.setId(id);
        app.setUpdateTime(new Date());
        app.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        applicationMapper.updata(app);
        //删除审批表
        auditMapper.delete(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("{}",applicationId);
        Application application = new Application();
        application.setId(applicationId);
        application.setUpdateTime(new Date());
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        applicationMapper.updata(application);
        Vehicle vehicle = new Vehicle();
        vehicle.setStatus("2");
        vehicle.setId(vehicleId);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }

    /**
     * 换车
     * @param applicationId
     */
    @Override
    public void applicationUp(Long applicationId) {
        log.debug("{}",applicationId);
        Application application = new Application();
        application.setId(applicationId);
        application.setUpdateTime(new Date());
        application.setStatus(ApplicationStatusEnum.END.getCode());
        applicationMapper.updata(application);
        Vehicle vehicle = new Vehicle();
        vehicle.setStatus("1");
        vehicle.setId(application.getVehicleId());
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
